//
//  DACommentTableViewCell.m
//  DAKnowledge
//
//  Created by DreamArts on 16/6/23.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DACommentTableViewCell.h"
#import "DALabel.h"
#import "Utility.h"
#import "UIViewAdditions.h"
#import "DAReplyModel.h"

@interface DACommentTableViewCell (){
    UILabel *messageLabel;
    DALabel *DAMessageLabel;
}
@end
@implementation DACommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
      
        [self creatDAMessageLabel];
    }
    return self;
}
- (void)creatMessageLabel{
    messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, [UIScreen mainScreen].bounds.size.width - 113, self.contentView.frame.size.height - 10)];
    messageLabel.font = [UIFont systemFontOfSize:12];
    messageLabel.numberOfLines = 0;
    messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
    CGSize size = [self sizeThatFits:CGSizeMake(messageLabel.frame.size.width, MAXFLOAT)];
    messageLabel.frame = CGRectMake(messageLabel.frame.origin.x, messageLabel.frame.origin.y, messageLabel.frame.size.width, size.height);
    messageLabel.textColor = [UIColor darkGrayColor];
    [self.contentView addSubview:messageLabel];
}

- (void)creatDAMessageLabel{
    DAMessageLabel = [[DALabel alloc]initWithFrame:CGRectMake(8, 5, [UIScreen mainScreen].bounds.size.width - 113, self.contentView.frame.size.height - 10)];
    DAMessageLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:DAMessageLabel];
   // CGFloat width = [UIScreen mainScreen].bounds.size.width - 113;
   // NSLog(@"%@", NSStringFromCGRect(self.contentView.frame));
   // NSLog(@"%@", NSStringFromCGRect(DAMessageLabel.frame));
    
}


//+ (instancetype)cellWithTableView:(UITableView *)tableView{
//    static NSString *identifier = @"CommetCell";
//    DACommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//    if (cell == nil) {
//        cell = [[DACommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//    }
//    
//    return cell;
//}
//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
//    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
//    if (self) {
//        self.commentLabel = [[DALabel alloc]initWithFrame:CGRectMake(8, 8, [UIScreen mainScreen].bounds.size.width - 113, self.contentView.frame.size.height - 10)];
//        self.commentLabel.font = [UIFont systemFontOfSize:12];
//        [self.contentView addSubview:self.commentLabel];
//        
//    }
//    return self;
//}

- (void)setReplyModel:(DAReplyModel *)replyModel{
    if (_replyModel != replyModel) {
        _replyModel = replyModel;
    }
    
        NSString *message = replyModel.replyContent;
        NSString *replyUserName = replyModel.replyUserName;
        NSString *replyToName = replyModel.replyToName;
        NSString *content;
        if ([replyToName isKindOfClass:[NSNull class]] || replyToName == nil) {
            content = [NSString stringWithFormat:@"%@：%@", replyUserName, message];
            NSMutableAttributedString *contentColor = [[NSMutableAttributedString alloc]initWithString:content];
            NSRange range;
            range = [content rangeOfString:replyUserName];
            [contentColor addAttribute:NSForegroundColorAttributeName value:[UIColor supportGreenColor] range:range];
            // self.commentLabel.attributedText = contentColor;
            //[self.commentLabel setHeightForLabel];
            //messageLabel.attributedText = contentColor;
            //[messageLabel sizeToFit];
            // [messageLabel setHeightForLabel];
            //[cell.commentLabel setText:content fitToContent:YES];
            
            DAMessageLabel.attributedText = contentColor;
            [DAMessageLabel setHeightForLabel];
            
        }else{
            if (replyUserName != nil && replyToName != nil) {
                NSString *reply = NSLocalizedString(@"reply", nil);
                content = [NSString stringWithFormat:@"%@%@%@：%@", replyUserName,reply, replyToName, message];
                NSMutableAttributedString *contentColor = [[NSMutableAttributedString alloc]initWithString:content];
                NSRange range;
                NSRange range2;
                range = [content rangeOfString:replyUserName];
                range2 = [content rangeOfString:replyToName];
                [contentColor addAttribute:NSForegroundColorAttributeName value:[UIColor supportGreenColor] range:range];
                [contentColor addAttribute:NSForegroundColorAttributeName value:[UIColor supportGreenColor] range:range2];
                // self.commentLabel.attributedText = contentColor;
                
                //[self.commentLabel setHeightForLabel];
                //  [cell.commentLabel setText:content fitToContent:YES];
                //messageLabel.attributedText = contentColor;
                // [messageLabel sizeToFit];
                //[messageLabel setHeightForLabel];
                
                DAMessageLabel.attributedText = contentColor;
                [DAMessageLabel setHeightForLabel];
                
            }else{
                NSLog(@"没走");
                
            }
        }


//    CGSize size = [self sizeThatFits:CGSizeMake(messageLabel.frame.size.width, MAXFLOAT)];
//    messageLabel.frame = CGRectMake(messageLabel.frame.origin.x, messageLabel.frame.origin.y, messageLabel.frame.size.width, size.height);
    
   // messageLabel.text = message;
    
    
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
